﻿using System;
using UnityEngine;
using UnityEngine.Events;
using Yoozoo.UI;
using Yoozoo.UI.YGUI;

public class YTextBinder : BinderBase
{
    public enum AttributeType : int
    {
        m_LetterSpacing,
        text,
        letterSpacing,
        numberMode,
        duration,
        onTweenComplete,
        number,
        supportRichText,
        resizeTextForBestFit,
        resizeTextMinSize,
        resizeTextMaxSize,
        alignByGeometry,
        fontSize,
        lineSpacing,
        onCullStateChanged,
        maskable,
        isMaskingGraphic,
        color,
        raycastTarget,
        enabled,
    }

    private YText _target;

    public YText Target => _target;

    public YTextBinder(UIExpansion owner, LinkerConfig config) : base(owner, config)
    {
    }

    public YTextBinder()
    {
        
    }

    public override void Dispose()
    {
        _target = null;
        base.Dispose();
    }

    public override void Init(LinkerConfig config)
    {
        GameObject targetGo = _owner.GetStoredGameObject(config.StoredGameObjectIndex);
        if (targetGo == null)
        {
            return;
        }
        _target = targetGo.GetComponent<YText>();
        _active = _target != null;
    }
    public override void SetSingle(float value)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.m_LetterSpacing:
                _target.m_LetterSpacing = value;
            break;
            case AttributeType.letterSpacing:
                _target.letterSpacing = value;
            break;
            case AttributeType.duration:
                _target.duration = value;
            break;
            case AttributeType.number:
                _target.number = value;
            break;
            case AttributeType.lineSpacing:
                _target.lineSpacing = value;
            break;
            default:
            break;
        }
        base.SetSingle(value);
    }

    public override void SetString(string value)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.text:
                _target.text = value;
            break;
            default:
            break;
        }
        base.SetString(value);
    }

    public override void SetBoolean(bool value)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.numberMode:
                _target.numberMode = value;
            break;
            case AttributeType.supportRichText:
                _target.supportRichText = value;
            break;
            case AttributeType.resizeTextForBestFit:
                _target.resizeTextForBestFit = value;
            break;
            case AttributeType.alignByGeometry:
                _target.alignByGeometry = value;
            break;
            case AttributeType.maskable:
                _target.maskable = value;
            break;
            case AttributeType.isMaskingGraphic:
                _target.isMaskingGraphic = value;
            break;
            case AttributeType.raycastTarget:
                _target.raycastTarget = value;
            break;
            case AttributeType.enabled:
                _target.enabled = value;
            break;
            default:
            break;
        }
        base.SetBoolean(value);
    }

    public override void SetAction2(UnityAction action)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.onTweenComplete:
                _target.onTweenComplete.AddListener(action);
            break;
            default:
            break;
        }
        base.SetAction2(action);
    }

    public override void RemoveAction2(UnityAction action)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.onTweenComplete:
                _target.onTweenComplete.RemoveListener(action);
            break;
            default:
            break;
        }
        base.RemoveAction2(action);
    }

    public override void SetInt32(int value)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.resizeTextMinSize:
                _target.resizeTextMinSize = value;
            break;
            case AttributeType.resizeTextMaxSize:
                _target.resizeTextMaxSize = value;
            break;
            case AttributeType.fontSize:
                _target.fontSize = value;
            break;
            default:
            break;
        }
        base.SetInt32(value);
    }

    public override void SetActionBoolean(UnityAction<bool> action)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.onCullStateChanged:
                _target.onCullStateChanged.AddListener(action);
            break;
            default:
            break;
        }
        base.SetActionBoolean(action);
    }

    public override void RemoveActionBoolean(UnityAction<bool> action)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.onCullStateChanged:
                _target.onCullStateChanged.RemoveListener(action);
            break;
            default:
            break;
        }
        base.RemoveActionBoolean(action);
    }

    public override void SetColor(Color value)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.color:
                _target.color = value;
            break;
            default:
            break;
        }
        base.SetColor(value);
    }

    public override void RemoveAllAction()
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.onTweenComplete:
                _target.onTweenComplete.RemoveAllListeners();
            break;
            case AttributeType.onCullStateChanged:
                _target.onCullStateChanged.RemoveAllListeners();
            break;
            default:
            break;
        }
    }

}
